import { _props } from "./canvas.js";
import { polarPoints } from "../math/points/index.js";
import { _circle } from "./circle.js";
import { _path } from './path.js'
import { _text } from "./text.js";
import { _mirror } from "./mirror.js";
import { rayMix } from '../math/arrayMix.js'
import { _dis, _mid } from '../math/index.js'
import { _transform } from '../math/transform.js'


export const _polygon = (ctx, options, props) => {
    let {
        o = [400, 300], r = 100, a = 0,
        n = 6,
        centre,
        vertex,
        labels,
        radius,
        excircle,
        incircle,
        mirror,
        color,
        transform
    } = options
    let points = polarPoints({
        o, r, a,
        n
    })
    // 变形函数
    points = _transform(points, transform)

    ctx.save();
    _props(ctx, props)
    _path(ctx, { points })
    // 顶点
    if (vertex) {
        points.forEach(t => {
            _circle(ctx, {
                o: t, r: 3
            }, {
                fill: 'red',
                stroke: 'none',
                ...props
            })
        })
    }
    // 文字
    if (labels) {
        points.forEach((t, i) => {
            _text(ctx, {
                x: t[0],
                y: t[1],
                text: i
            })
        })
    }
    // 中心点
    if (centre) {
        _circle(ctx, {
            o, r: 3
        }, {
            fill: 'red',
            stroke: 'none',
            ...props
        })
    }
    // 半径
    if (radius) {
        let rps = rayMix(o, points)
        _path(ctx, { points: rps, closed: false })
    }
    // 外切圆
    if (excircle) {
        _circle(ctx, {
            o, r
        }, {
            fill: 'none',
            stroke: 'red',
            ...props
        })
    }
    // 内切圆
    if (incircle) {
        let r = _dis(o, _mid.apply(null, points.slice(0, 2)))
        _circle(ctx, {
            o, r
        }, {
            fill: 'none',
            stroke: 'red',
            ...props
        })
    }
    // 镜像
    if (mirror) {
        _mirror(ctx, {
            points,
            o, r, n, a,
            centre,
            vertex,
            labels,
            radius,
            excircle,
            incircle,
            mirror,
            color,
            transform
        })
    }

    ctx.restore();
}
